package com.hspedu.jdbc.result_;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/4/8
 * @time 13:09
 */
public class PreparedStatement_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
    }

    public static void test1() throws IOException, ClassNotFoundException, SQLException {
        Scanner scanner = new Scanner(System.in);

        //让用户输入管理员名和密码
        //next(): 当接收到 空格或者 '就是表示结束
        System.out.print("请输入第一个管理员的名字: ");
        String admin_name = scanner.nextLine();
        System.out.print("请输入第一个管理员的密码: ");
        String admin_pwd = scanner.nextLine();

        System.out.print("请输入第二个管理员的名字: ");
        String admin_name2 = scanner.nextLine();
        System.out.print("请输入第二个管理员的密码: ");
        String admin_pwd2 = scanner.nextLine();

        System.out.print("请输入第三个管理员的名字: ");
        String admin_name3 = scanner.nextLine();
        System.out.print("请输入第三个管理员的密码: ");
        String admin_pwd3 = scanner.nextLine();

        System.out.print("请输入第四个管理员的名字: ");
        String admin_name4 = scanner.nextLine();
        System.out.print("请输入第四个管理员的密码: ");
        String admin_pwd4 = scanner.nextLine();

        System.out.print("请输入第五个管理员的名字: ");
        String admin_name5 = scanner.nextLine();
        System.out.print("请输入第五个管理员的密码: ");
        String admin_pwd5 = scanner.nextLine();

        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("D:\\workspace_idea\\java0\\hspedu\\chapter25\\src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1. 注册驱动
        Class.forName(driver);

        //2. 得到连接
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql = "insert into admin values (?, ?),(?, ?),(?, ?),(?, ?),(?, ?);";
        //3. 得到PreparedStatement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,admin_name);
        preparedStatement.setString(2,admin_pwd);
        preparedStatement.setString(3,admin_name2);
        preparedStatement.setString(4,admin_pwd2);
        preparedStatement.setString(5,admin_name3);
        preparedStatement.setString(6,admin_pwd3);
        preparedStatement.setString(7,admin_name4);
        preparedStatement.setString(8,admin_pwd4);
        preparedStatement.setString(9,admin_name5);
        preparedStatement.setString(10,admin_pwd5);
        int affectRows = preparedStatement.executeUpdate();
        System.out.println(affectRows);

        preparedStatement.close();
        connection.close();
    }

    public static void test2() throws IOException, ClassNotFoundException, SQLException {
        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("D:\\workspace_idea\\java0\\hspedu\\chapter25\\src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1. 注册驱动
        Class.forName(driver);

        //2. 得到连接
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql = "update admin set name = 'king' where name = 'tom';";
        //3. 得到PreparedStatement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        int affectRows = preparedStatement.executeUpdate();
        System.out.println("affectRows = " + affectRows);
    }

    public static void test3() throws SQLException, ClassNotFoundException, IOException {
        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("D:\\workspace_idea\\java0\\hspedu\\chapter25\\src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1. 注册驱动
        Class.forName(driver);

        //2. 得到连接
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql = "delete from admin where name = 'jack';";
        //3. 得到PreparedStatement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        int affectRows = preparedStatement.executeUpdate();
        System.out.println("affectRows = " + affectRows);
    }

    public static void test4() throws ClassNotFoundException, SQLException, IOException {
        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("D:\\workspace_idea\\java0\\hspedu\\chapter25\\src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1. 注册驱动
        Class.forName(driver);

        //2. 得到连接
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql = "select * from admin";
        //3. 得到PreparedStatement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            String name = resultSet.getString(1);
            String pwd = resultSet.getString(2);
            System.out.print(name +" "+pwd+"\n");
        }

        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
